﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using ServiceCore.Tool.ES;
using ServiceCore.Tool.Post;
using ServiceCore.Tool.TIM.chat_message;
using ServiceCore.Tool.TIM.im_open_login_svc;
using ServiceCore.Tool.TIM.sns;
using System;
using System.Collections.Generic;
using System.Text;
using tencentyun;
using static ServiceCore.Tool.TIM.chat_message.MsgBody;

namespace ServiceCore.Tool.TIM
{
    public class tim_service : Itim_service
    {
        private readonly IConfiguration AppSettingConfig;
        private readonly IEs_Log_Helper _log;
        private readonly IHttpContextAccessor _context;
        public tim_service(IConfiguration configuration, IEs_Log_Helper Log, IHttpContextAccessor Context)
        {
            AppSettingConfig = configuration;
            _log = Log;
            _context = Context;
        }
        /// <summary>
        /// 定义sdkappid
        /// </summary>
        public int sdkappid { get { return Convert.ToInt32(AppSettingConfig["tim:sdkappid"]); } }
        /// <summary>
        /// 定义key
        /// </summary>
        public string key { get { return AppSettingConfig["tim:key"]; } }
        /// <summary>
        /// 管理员账号
        /// </summary>
        public string app_admin { get { return AppSettingConfig["tim:app_admin"]; } }
        /// <summary>
        /// 计算 UserSig
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public string get_user_sign(string userid)
        {
            TLSSigAPIv2 api = new TLSSigAPIv2(sdkappid, key);
            string sig = api.GenSig(userid);
            return sig;
        }
        /// <summary>
        /// 创建连接并请求
        /// </summary>
        /// <returns></returns>
        public K http_client<K, T>(base_post_model<T> post_params)
        {
            try
            {
                string post_result = "";
                string url = $"https://console.tim.qq.com/{post_params.ver}/{post_params.servicename}/{post_params.command}?sdkappid={sdkappid}&identifier={app_admin}&usersig={get_user_sign(app_admin)}&random={new Random().Next(0, 999999999)}&contenttype=json";
                post_result = Http_Helper.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(post_params.post_inner));
                return Newtonsoft.Json.JsonConvert.DeserializeObject<K>(post_result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region  账号管理
        /// <summary>
        /// 导入单个账号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public account_import_result account_import(account_import_post model)
        { 
            return http_client<account_import_result, account_import_post>(new account_import()
            {
                post_inner = model
            }); 
        }
        /// <summary>
        /// 导入多个账号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public multiaccount_import_result multiaccount_import(multiaccount_import_post model)
        {
            return http_client<multiaccount_import_result, multiaccount_import_post>(new multiaccount_import()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 删除账号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public account_delete_result account_delete(account_delete_post model)
        {
            return http_client<account_delete_result, account_delete_post>(new account_delete()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 查询账号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public account_check_result account_check(account_check_post model)
        {
            return http_client<account_check_result, account_check_post>(new account_check()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 失效帐号登录态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public kick_result kick(kick_post model)
        {
            return http_client<kick_result, kick_post>(new kick()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 查询帐号在线状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public querystate_result querystate(querystate_post model)
        { 
            return http_client<querystate_result, querystate_post>(new querystate()
            {
                post_inner = model
            });
        }
        #endregion

        #region 单聊消息
        /// <summary>
        /// 单发单聊信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public sendmsg_result sendmsg(sendmsg_post model)
        {
            return http_client<sendmsg_result, sendmsg_post>(new sendmsg()
            {
                post_inner = model
            });
        } 
        /// <summary>
        /// 群发单聊信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public batchsendmsg_result batchsendmsg(batchsendmsg_post model)
        {
            return http_client<batchsendmsg_result, batchsendmsg_post>(new batchsendmsg()
            {
                post_inner = model
            });
        }

        /// <summary>
        /// 查询单聊消息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public admin_getroammsg_result admin_getroammsg(admin_getroammsg_post model)
        {
            return http_client<admin_getroammsg_result, admin_getroammsg_post>(new admin_getroammsg()
            {
                post_inner = model
            });
        } 
        /// <summary>
        /// 撤回单聊消息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public admin_msgwithdraw_result admin_msgwithdraw(admin_msgwithdraw_post model)
        {
            return http_client<admin_msgwithdraw_result, admin_msgwithdraw_post>(new admin_msgwithdraw()
            {
                post_inner = model
            });
        }
        #endregion 

        #region 关系链管理
        /// <summary>
        /// 添加好友
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public friend_add_result friend_add(friend_add_post model)
        {
            return http_client<friend_add_result, friend_add_post>(new friend_add()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 导入好友
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public friend_import_result friend_import(friend_import_post model)
        {
            return http_client<friend_import_result, friend_import_post>(new friend_import()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public friend_delete_result friend_delete(friend_delete_post model)
        {
            return http_client<friend_delete_result, friend_delete_post>(new friend_delete()
            {
                post_inner = model
            });
        }
        /// <summary>
        /// 更新好友
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public friend_update_result friend_delete(friend_update_post model)
        {
            return http_client<friend_update_result, friend_update_post>(new friend_update()
            {
                post_inner = model
            });
        } 


        #endregion

    }
}
